package gui_classes;

import java_persistence.AppointmentMapper;
import java_persistence.DoctorMapper;
import java_persistence.NurseMapper;
import java_persistence.OrdersMapper;
import java_persistence.TreatmentRecordMapper;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import domain.Appointment;
import domain.Doctor;
import domain.Invoice;
import domain.Nurse;
import domain.Orders;
import domain.Patient;
import domain.TreatmentRecord;
import javax.swing.JComboBox;

import java.util.Iterator;
import java.util.List;
import javax.swing.JTextPane;

/**
 * This class represents a treatment screen for a patient
 * @author SCIUUG Faults
 *
 */
public class TreatmentScreen extends PatientAppointmentRequestPanel {
	
	private JComboBox NurseBox;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	private JTextField textField_8; 
	
	private List<Nurse> nurses;
	private JTextField textField;
	
	private JTextArea textArea_1;
	private Invoice invoice;
	private Patient patient;
	private Orders order;

	/**
	 * Create the panel.
	 */
	public TreatmentScreen(Patient pat, DERPBase callingPage) {
		super(pat);
		setLayout(null);
		setPreferredSize(new Dimension(450, 350));
		super.setTitle("Treatment Screen");
		super.removeButton();
		
		this.patient = pat;
		
		JLabel lblNewLabel_1 = new JLabel("Nurse");
		lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblNewLabel_1.setBounds(215, 39, 46, 14);
		add(lblNewLabel_1);
				
		JLabel lblVitalSigns = new JLabel("Vital Signs:");
		lblVitalSigns.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblVitalSigns.setBounds(10, 114, 68, 14);
		add(lblVitalSigns);
		
		JLabel lblHeight = new JLabel("Height");
		lblHeight.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblHeight.setBounds(97, 117, 46, 14);
		add(lblHeight);
		
		JLabel lblWeight = new JLabel("Weight");
		lblWeight.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblWeight.setBounds(97, 138, 46, 14);
		add(lblWeight);
		
		textField_4 = new JTextField();
		textField_4.setBounds(143, 114, 60, 20);
		add(textField_4);
		textField_4.setColumns(10);
		
		textField_5 = new JTextField();
		textField_5.setBounds(143, 135, 60, 20);
		add(textField_5);
		textField_5.setColumns(10);
		
		JLabel lblTemperature = new JLabel("Pulse");
		lblTemperature.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblTemperature.setBounds(97, 159, 46, 14);
		add(lblTemperature);
		
		textField_6 = new JTextField();
		textField_6.setBounds(143, 156, 60, 20);
		add(textField_6);
		textField_6.setColumns(10);
		
		JLabel lblTemperature_1 = new JLabel("Temperature");
		lblTemperature_1.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblTemperature_1.setBounds(220, 114, 101, 14);
		add(lblTemperature_1);
		
		JLabel lblBloodPressure = new JLabel("Blood Pressure D:");
		lblBloodPressure.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblBloodPressure.setBounds(220, 135, 117, 14);
		add(lblBloodPressure);
		
		textField_7 = new JTextField();
		textField_7.setColumns(10);
		textField_7.setBounds(339, 112, 86, 20);
		add(textField_7);
		
		textField_8 = new JTextField();
		textField_8.setColumns(10);
		textField_8.setBounds(339, 135, 86, 20);
		add(textField_8);
		
		JLabel lblDiagnosis = new JLabel("Diagnosis:");
		lblDiagnosis.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblDiagnosis.setBounds(10, 187, 107, 14);
		add(lblDiagnosis);
		
		textArea_1 = new JTextArea();
		textArea_1.setBounds(10, 202, 193, 87);
		add(textArea_1);
		
		JButton btnAdd = new JButton("Add");
		btnAdd.setBounds(42, 301, 117, 25);
		btnAdd.addActionListener(new AddTListener());
		add(btnAdd);
		
		
		nurses = GUI_Main.getSession().getMapper(NurseMapper.class).get_nurses();

		int i=0;
		String[] nurStr = new String[nurses.size()];
		for ( Iterator<Nurse> nursesIter = nurses.iterator(); nursesIter.hasNext(); ) {
			Nurse curn = nursesIter.next();
			nurStr[i]=curn.getName() + " " + curn.getUsername();
			i++;
		}
				
		NurseBox = new JComboBox(nurStr);
		NurseBox.setBounds(286, 33, 107, 20);
		add(NurseBox);
		
		JLabel lblBloodPressureS = new JLabel("Blood Pressure S:");
		lblBloodPressureS.setFont(new Font("Dialog", Font.BOLD, 11));
		lblBloodPressureS.setBounds(220, 158, 117, 14);
		add(lblBloodPressureS);
		
		textField = new JTextField();
		textField.setBounds(339, 156, 86, 19);
		add(textField);
		textField.setColumns(10);
		
		JButton btnGenerateInvoice = new JButton("Generate Invoice");
		btnGenerateInvoice.setBounds(227, 301, 176, 25);
		btnGenerateInvoice.addActionListener(new generateInvoiceListener());
		add(btnGenerateInvoice);
		// Only Doctors can do this
		
		if(callingPage.getClass().isAssignableFrom(DoctorPage.class))    
		{
			JButton btnAddDoctorsOrders = new JButton("Add Doctor's Orders");
			btnAddDoctorsOrders.setBounds(227, 225, 198, 25);
			btnAddDoctorsOrders.addActionListener(new AddOrdersListener());
			add(btnAddDoctorsOrders);
		}

	}
	
	public JComboBox getNurseBox()
	{
		return this.NurseBox;
	}
	
	public JTextField getBPD()
	{
		return this.textField_8;
	}
	
	public void setBPD(String t)
	{
		this.textField_8.setText(t);
	}
	
	public JTextField getBPS()
	{
		return this.textField;
	}
	
	public void setBPS(String t)
	{
		this.textField.setText(t);
	}
	
	public JTextArea getDiagnosis()
	{
		return this.textArea_1;
	}
	
	public void setDiagnosis(String t)
	{
		this.textArea_1.setText(t);
	}
		
	public JTextField getHt()
	{
		return this.textField_4;
	}
	
	public void setHt(String t)
	{
		this.textField_4.setText(t);
	}
		
	public JTextField getTemp()
	{
		return this.textField_7;
	}
	public void setTemp(String t)
	{
		this.textField_7.setText(t);
	}
	
	public JTextField getWt()
	{
		return this.textField_5;
	}
	
	public void setWt(String t)
	{
		this.textField_5.setText(t);
	}
	
	public JTextField getPulse()
	{
		return this.textField_6;
	}
	
	public void setPulse(String t)
	{
		this.textField_6.setText(t);
	}
	
	public void setInvoice(Invoice i)
	{
		this.invoice = i;
	}
	
	public Invoice getInvoice()
	{
		return this.invoice;
	}
	
	public void setOrders(Orders o)
	{
		this.order = o;
	}
	
	public Orders getOrders()
	{
		return this.order;
	}
	
	public void display()
	{
		GUI_Main.replacePanel(this);
	}
	
	private class AddOrdersListener implements ActionListener{
		public void actionPerformed(ActionEvent event){
			GUI_Main.oscreen = new OrdersScreen();
			GUI_Main.oscreen.setPrevious(GUI_Main.viewpinfo);
			GUI_Main.oscreen.display();
		}
	}
	
	private class generateInvoiceListener implements ActionListener{
		public void actionPerformed(ActionEvent event)
		{
			GUI_Main.iscreen = new InvoiceScreen();
			GUI_Main.iscreen.setPrevious(GUI_Main.viewpinfo);
			GUI_Main.iscreen.display();
		}
	}

	private class AddTListener implements ActionListener{
		public void actionPerformed(ActionEvent event)
		{
			
			/* Joseph: I did as much as I could to add a treatment record to the database. I am not sure
			 * about how to complete this part of the code. Can you complete it?
			 * 
			 * Pratik: No, I can't.  The treatment record has to be associated with an appointment but in the database and for
			 * our java object model to make sense.  The TreatmentRecord object is supposed to be a member of an Appointment
			 * object, and I need the patient name and the date of the appointment to commit it to the database at the very least.
			 * Ideally, I'd have access to the whole Appointment, but it's not actually necessary.
			 * 
			 * Figure out how to get that object into that method and you've already got half the call commented out (new_record.create_treatment_record(a, session)),
			 * which needs a semicolon.  If the current appointment is a and you replace session with GUI_Main.getSession(), that's it.  Then
			 * just call GUI_Main.getSession().commit(); and that's it.
			 * 
			 * I'm not sure what needs to change to get access to that appointment, otherwise I'd just do it myself.
			 * 
			 * 
			 */
			System.out.println("Added to Treatment Record");
			
			String[] myArray = new String[11];
			
			String doc_name = (String) GUI_Main.tscreen.getDoctorBox().getSelectedItem();
			String[] doc_names = doc_name.split(" ");
			String doc_username = doc_names[doc_names.length-1];
			Doctor cur = GUI_Main.getSession().getMapper(DoctorMapper.class).get_doctor_by_username(doc_username);
			
			String nur_name = (String) GUI_Main.tscreen.getNurseBox().getSelectedItem();
			String[] nur_names = nur_name.split(" ");
			String nur_username = nur_names[nur_names.length-1];
			Nurse curn = GUI_Main.getSession().getMapper(NurseMapper.class).get_nurse_by_username(nur_username);
			
			String day = GUI_Main.tscreen.getTextField1().getText();
			String time = GUI_Main.tscreen.getTextField2().getText();
			String date = day + " "+ time;
			String patient_username = patient.getUsername();
			
			TreatmentRecord new_record = new TreatmentRecord();
			new_record.setAttending_doctor(cur);
			new_record.setAttending_nurse(curn);
			new_record.setBp_diastolic(Integer.parseInt(GUI_Main.tscreen.getBPD().getText()));
			new_record.setBp_systolic(Integer.parseInt(GUI_Main.tscreen.getBPS().getText()));
			new_record.setDiagnosis(GUI_Main.tscreen.getDiagnosis().getText());
			new_record.setHeight_inches(Integer.parseInt(GUI_Main.tscreen.getHt().getText()));
			new_record.setInvoice(invoice);
			new_record.setNotes(GUI_Main.tscreen.getTextArea().getText());			
			new_record.setOrders(GUI_Main.tscreen.getOrders());
			new_record.setTemperature(Float.parseFloat(GUI_Main.tscreen.getTemp().getText()));
			new_record.setWeight_pounds(Integer.parseInt(GUI_Main.tscreen.getWt().getText()));
			
			

			
			
			
			// Joseph: use Strings date and patient_username;
	//		new_record.create_treatment_record(a, session)
			
			Appointment a = new Appointment(date, "", null, patient, null); // dummy values.  this object doesn't do anything important except let the date and patient_username get into the create_treatment_record method call correctly.
			
			new_record.create_treatment_record(a, GUI_Main.getSession());
			GUI_Main.getSession().commit();
					
			//GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).create_treatment_record(new_record.getAttending_doctor(), pu, du, nu, bps, bpd, pulse, temp, hi, wp, diag, n);
			GUI_Main.tscreen.displayPrev();


		}
	}


}
